package com.ruoyi.project.user.company.controller;


import com.ruoyi.common.constant.SqlOrder;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.user.company.domain.Company;
import com.ruoyi.project.user.company.service.CompanyService;
import com.ruoyi.project.user.host.domain.Host;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 婚庆公司controller
 *
 * @author xf
 */
@Controller
@RequestMapping("/user/company")
public class CompanyController extends BaseController {

    private String prefix = "user/company";

    @Autowired
    private CompanyService companyService;


    @RequiresPermissions("user:company:view")
    @GetMapping()
    public String company(){
        return prefix +"/company";

    }


    /**
     * 查询全部公司
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions("user:company:list")
    public TableDataInfo list(Company company, Integer companyOrderVolumeOrderNum){

        startPage(SqlOrder.getSqlOrder(companyOrderVolumeOrderNum));
        List<Company> companyList = companyService.selectAll(company);
        return getDataTable(companyList);

    }

    @GetMapping("/add")
    public String add(){
        return prefix +"/add";
    }

    /**
     * 添加公司
     * @param company
     * @param confirmPassword
     * @return
     */
    @PostMapping("/add")
    @RequiresPermissions("user:company:add")
    @ResponseBody
    public AjaxResult addCompany(Company company,String confirmPassword){

        return toAjax(companyService.insertCompany(company, confirmPassword));

    }

    /**
     * 手机验证
     * @param companyPhone
     * @return
     */
    @PostMapping("/checkPhone")
    @ResponseBody
    public Long checkPhone(String companyPhone){
        return companyService.checkCompanyPhone(companyPhone);
    }

    /**
     * 邮箱验证
     * @param companyEmail
     * @return
     */
    @PostMapping("/checkEmail")
    @ResponseBody
    public Long checkEmail(String companyEmail){
        return companyService.checkCompanyEmail(companyEmail);
    }


    /**
     * 删除公司
     * @param ids
     * @return
     */
    @RequiresPermissions("user:company:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult deleteCompany(Long [] ids){

        return toAjax(companyService.deleteCompany(ids));
    }

    @Log(title = "婚庆公司列表", businessType = BusinessType.EXPORT)
    @RequiresPermissions("user:company:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Company company , Integer companyOrderVolumeOrderNum){
        List<Company> companyList = companyService.selectAllExcel(company,companyOrderVolumeOrderNum);

        ExcelUtil<Company> util = new ExcelUtil<Company>(Company.class);

        return util.exportExcel(companyList,"婚庆公司");
    }

    @RequiresPermissions("user:company:state")
    @PostMapping("/ChangeState")
    @ResponseBody
    public AjaxResult changeState(Company company){

        return toAjax(companyService.updateStateCompany(company));
    }

    /**
     * 婚庆公司修改  用于页面回显
     * @param companyId
     * @param model
     * @return
     */
    @RequiresPermissions("user:company:edit")
    @GetMapping("/edit/{companyId}")
    public String echoCompany(@PathVariable("companyId") Long companyId, Model model){

        model.addAttribute("company",companyService.selectOneByCompany(companyId));
        return prefix + "/edit";
    }



    /**
     * 密码验证
     * @param
     * @return
     */
    @PostMapping("/checkPassword")
    @ResponseBody
    public Long checkPassword(String beforePassword,Long companyId){

        return companyService.checkCompanyPassword(beforePassword,companyId) ;

    }


    /**
     * 修改该婚庆公司
     * @param company
     * @param newPassword
     * @return
     */
    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions("user:company:edit")
    public AjaxResult changeCompany(Company company,String newPassword){

        return toAjax(companyService.changeCompany(company, newPassword));
    }


    /**
     * 策划师列表
     * @param companyId
     * @param mmap
     * @return
     */
    @RequiresPermissions("user:company:planner")
    @GetMapping("/planner/{companyId}")
    public String planner(@PathVariable("companyId") Long companyId, ModelMap mmap){
        mmap.put("plannerCompanyId",companyId);
        return prefix + "/planner/planner";

    }
}



